package net.lesscoding.server.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import net.lesscoding.session.SessionFactory;

/**
 * @author eleven
 * @date 2024/10/31 17:45
 * @apiNote
 */
@Slf4j
@ChannelHandler.Sharable
public class QuitHandler extends ChannelInboundHandlerAdapter {
    /**
     * 连接断开时触发
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        SessionFactory.session().unbind(ctx.channel());
        log.debug("{} 已经断开连接", ctx.channel());
        super.channelInactive(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        SessionFactory.session().unbind(ctx.channel());
        log.info("{} 异常断开连接, 异常信息{}", ctx.channel(), cause.getMessage());
        super.exceptionCaught(ctx, cause);
    }
}
